package main

import (
	"fmt"
	"net"
	"time"
)

func main() {
	// 创建连接
	socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
		IP:   net.IPv4(255, 255, 255, 255),
		Port: 61005,
	})
	if err != nil {
		fmt.Println("连接失败!", err)
		return
	}
	defer socket.Close()

	// 发送数据
	/*senddata := []byte("hello server!")
	_, err = socket.Write(senddata)
	if err != nil {
		fmt.Println("发送数据失败!", err)
		return
	}
	time.Sleep(time.Duration(2)*time.Second)
*/



	go func() {
		// 接收数据
		for {
			data := make([]byte, 64)
			read, remoteAddr, err := socket.ReadFromUDP(data)
			if err != nil {
				fmt.Println("读取数据失败!", err)
				//return
			}
			fmt.Println(read, remoteAddr)
			fmt.Println("%s\n", data)
		}

	}()

	for{
		// 发送数据
		senddata := []byte{23,64,0,0,68,185,77,13,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
		_, err = socket.Write(senddata)
		if err != nil {
			fmt.Println("发送数据失败!", err)
			//return
		}
		fmt.Println("发送数据!", senddata)
		time.Sleep(time.Duration(5)*time.Second)
	}
}